﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator.ConsoleCalc
{

    class Calculator: IExpressionCalculator
    {
		protected Dictionary<FunctionType, Functions.Function> Operations = new Dictionary<FunctionType, Functions.Function>();

		public Calculator()
		{
			Operations.Add(FunctionType.Multiply, new Functions.MultiplyFunction());
			Operations.Add(FunctionType.Divide, new Functions.DivideFunction());
			Operations.Add(FunctionType.Plus, new Functions.AddFunction());
			Operations.Add(FunctionType.Minus, new Functions.SubstructFunction());
		}

		/// <summary>
		/// Посчитать 
		/// </summary>
		/// <param name="exp"></param>
		/// <returns></returns>
        public double Calculate(Expression exp)
		{
			Functions.Function func;
			Operations.TryGetValue(exp.Function, out func);
			if (func == null)
				throw new CalculatorException(String.Format("Операция {0} неподдерживается калькулятором", exp.Function));
		
			return func.ExecuteOperation(exp.FirstOperand, exp.SecondOperand);
        }
    }
}
